<?php
class NewsController extends AppController{
    
    public $slug = array(
                    '71' => 'tin-tuc',
                    '100' => 'me-va-be',
                    '102' => 'khuyen-mai',
                    '103' => 'tuyen-dung'
                    );
    
    public function index($parent_code = null, $child_code = null)
    {
        $lang_code = $this->Session->read('lang_code');
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $this->loadModel('Article'); $this->Article->lang_code = $lang_code;
        
        
        if($parent_code == null)
            $this->redirect(array('controller' => 'intro', 'action' => 'index'));
        
        $parent_id = 0; $child_id = 0;
        if($parent_code != null)
        {
            $parent_id = $this->Category->get_category_id_by_code($parent_code);
            if($parent_id == 0)
                $this->redirect(array('controller' => 'intro', 'action' => 'index'));
            $parent_info = $this->Category->get_info_category_by_id($parent_id);
        }
        if($child_code != null)
        {
            $child_id = $this->Category->get_category_id_by_code($child_code);
            $child_info = $this->Category->get_info_category_by_id($child_id);
        } 
        $category_id = $child_id != 0 ? $child_id : $parent_id;
        
        
        $this->set('category_id', $category_id);
        $category_info = $this->Category->get_info_category_by_id($category_id);
        $this->set('category_info', $category_info);
        
        
        $list_category = $this->Category->get_all_id_category();
        $filter_category = $this->Category->filter_category($list_category);
        $in_array_cate = $this->Category->filter_category_by_id($filter_category, $category_id);
        

        $number_page = $this->Article->get_number_page_article_by_category_id($in_array_cate);

        $page_current = 1; $start = 0; $limit = Configure::read('post_per_page');
        if(isset($_GET['page']))
        {
            $page_current = $_GET['page'];
            $page_current = max(1, round($page_current));
            $page_current = min($number_page, $page_current);
        }

        $this->set('number_page', $number_page);
        $this->set('page_current', $page_current);
        
        if($page_current != '')
        {
            $start = ($page_current - 1) * $limit;
        }
        $artiles = $this->Article->get_article_by_category_id($in_array_cate, $start, $limit);
        $this->set('artiles', $artiles);
        
        
        if($child_id == 0)
        {
            $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link'].'.html');
            $this->set('link_page', $parent_info['link']['link'].'.html');
        }
        else
        {
            $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link'].'.html');
            $breadcrumbs[] = array('title' => $child_info['category_desc']['title'], 'link' => $parent_info['link']['link'] .'/'. $child_info['link']['link'].'.html');
            $this->set('link_page', $parent_info['link']['link'] .'/'. $child_info['link']['link'].'.html');
        }
        $this->set('breadcrumbs', $breadcrumbs);
        
        if($child_id != 0)
            $cat = $child_info;
        else
            $cat = $parent_info;

        $meta_title = $cat['category_desc']['meta_title'];
        $meta_keywords = $cat['category_desc']['meta_keywords'];
        $meta_description = $cat['category_desc']['meta_description'];
        $this->set('meta_title', $meta_title); $this->set('meta_keywords', $meta_keywords); $this->set('meta_description', $meta_description);

        $has_html = '.html';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = $parent_info['category_desc']['title'];
        $header_menu_sidebar['link'] = $parent_info['link']['link'];
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = $this->Category->get_category_by_code($parent_code);
        $this->set('menu_sidebar', $menu_sidebar);

    }
    
    public function detail($slug = null, $category_id = null)
    {
        $lang_code = $this->Session->read('lang_code');
        $this->loadModel('Article'); $this->Article->lang_code = $lang_code;
        $this->loadModel('Link'); $this->Link->lang_code = $lang_code;
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $link = $this->Link->get_link($slug, $lang_code);
        if(!empty($link))
        {
            $article_id = $link['post_id'];
            
            $article = $this->Article->get_article_by_article_id($article_id);
            $this->set('article', $article);
            
            
            $meta_title = $article[0]['article_desc']['meta_title'];
            $meta_keywords = $article[0]['article_desc']['meta_keywords'];
            $meta_description = $article[0]['article_desc']['meta_description'];
            $this->set('meta_title', $meta_title); $this->set('meta_keywords', $meta_keywords); $this->set('meta_description', $meta_description);
        }
        
        $parent_id = $this->Category->get_parent_cate_by_child_cate(1,$category_id);
        $parent_info = array();
        if($parent_id != $category_id)
        {
            $parent_info = $this->Category->get_info_category_by_id($parent_id);
            $category_info = $this->Category->get_info_category_by_id($category_id);
            $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link'] .'.html');
            $breadcrumbs[] = array('title' => $category_info['category_desc']['title'], 'link' => $parent_info['link']['link'] .'/'. $category_info['link']['link'] .'.html');
        }
        else
        {
            $category_info = $this->Category->get_info_category_by_id($category_id);
            $breadcrumbs[] = array('title' => $category_info['category_desc']['title'], 'link' => $category_info['link']['link'] .'.html');
        }
        $this->set('breadcrumbs', $breadcrumbs);
        
        $parent_info = !empty($parent_info) ? $parent_info : $category_info;
        
        $has_html = '.html';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = $parent_info['category_desc']['title'];
        $header_menu_sidebar['link'] = $parent_info['link']['link'];
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = $this->Category->get_category_by_code($parent_info['category']['category_code']);
        $this->set('menu_sidebar', $menu_sidebar);
        
    }
}